package com.wsz.security.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsz.security.pojo.Role;
import com.wsz.security.pojo.User;
import com.wsz.security.pojo.UserRole;
import com.wsz.security.service.RoleService;
import com.wsz.security.service.UserRoleService;
import com.wsz.security.service.UserService;
import com.wsz.security.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author wangs
* @description 针对表【user】的数据库操作Service实现
* @createDate 2022-11-19 13:31:43
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private RoleService roleService;

    @Override
    public List<Role> getRoleByUserId(Integer id) {

        List<UserRole> list = userRoleService.list(new QueryWrapper<UserRole>().eq("uid", id).select("rid"));

        List<Integer> roleIds = new ArrayList<>();

        list.forEach(i ->{
            roleIds.add(i.getRid());
        });

        return  roleService.list(new QueryWrapper<Role>().in("id", roleIds));
    }

    @Override
    public int updatebyUsername(String username, String newPassword) {
        return baseMapper.updatebyUsername(username, newPassword);
    }
}




